<template>
  <div class="params">
    <!-- 面包屑 -->
    <miAn-xie :betGao="betGao"></miAn-xie>
    <el-card>
      <!-- 注意 -->
      <el-alert
        title="注意：只允许为第三级分类设参数！"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 商品分类 -->
      <span class="demonstration">选择商品分类：</span>
      <el-cascader
        v-model="kong"
        :options="options"
        :props="azzX"
        clearable
        @change="get_click"
      ></el-cascader>
      {{ kong }}

      <!-- 标签页 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            size="mini "
            :disabled="jin"
            @click="dialogVisible = true"
            >添加参数</el-button
          >
          <!-- 表格 -->
          <el-table :data="tableData" border>
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-tag
                    :key="index"
                    v-for="(tag, index) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(scope.row,index)"
                  >
                    {{ tag }}
                  </el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scope.row.inputVisible"
                    v-model="scope.row.inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  >
                  </el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scope.row)"
                    >+ New Tag</el-button
                  >
                </el-form>
              </template>
            </el-table-column>
            <el-table-column label="#" type="index"> </el-table-column>
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" @click="categories_bj(scope.row)" circle></el-button>
              <el-button type="danger" icon="el-icon-delete" circle @click="categories_cha(scope.row)"></el-button>
              </template>
              
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            size="mini "
            :disabled="jin"
            @click="dialogVisible = true"
            >添加属性</el-button
          >
          <!-- 表格 -->
          <el-table :data="tableDataS" border>
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-tag
                    :key="index"
                    v-for="(tag, index) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(scope.row,index)"
                  >
                    {{ tag }}
                  </el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scope.row.inputVisible"
                    v-model="scope.row.inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  >
                  </el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scope.row)"
                    >+ New Tag</el-button
                  >
                </el-form>
              </template>
            </el-table-column>
            <el-table-column label="#" type="index"> </el-table-column>
            <el-table-column label="属性名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <el-button type="primary" icon="el-icon-edit" circle @click="categories_bj(scope.row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" circle @click="categories_cha(scope.row)"></el-button>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <!-- 添加动态或静态 -->
      <el-dialog
        :title="activeName == 'many' ? '添加动态参数' : '添加静态属性'"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item
            :label="activeName == 'many' ? '动态参数' : '静态属性'"
            prop="attr_name"
          >
            <el-input v-model="ruleForm.attr_name"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="resetForm('ruleForm')">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 编辑 -->
      <el-dialog
        :title="activeName == 'many' ? '编辑动态参数' : '编辑静态属性'"
        :visible.sync="dialogVi"
        width="50%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleFormS"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item
            :label="activeName == 'many' ? '动态参数' : '静态属性'"
            prop="attr_name"
          >
            <el-input v-model="ruleFormS.attr_name"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="resetForm('ruleForm')">取 消</el-button>
          <el-button type="primary" @click="submitFormS('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { get_categories } from "@/http/goods/categories";
import {
  categories_get,
  categories_put,
  categories_post,
  categories_delete_get,
  categories_delete
} from "@/http/goods/params";
export default {
  data() {
    return {
      // 面包屑
      betGao: [
        {
          path: "/home",
          title: "首页",
        },
        {
          path: "/params",
          title: "商品管理",
        },
        {
          path: "/params",
          title: "分类参数",
        },
      ],
      //   分类
      kong: [],
      options: [],
      azzX: {
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true,
      },
      //   标签页
      tableData: [],
      tableDataS: [],
      activeName: "many",
      // 添加静态或动态
      dialogVisible: false,
     
      ruleForm: {
        attr_name: "",
      },
      rules: {
        attr_name: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },

      // 
      dialogVi:false,
      ruleFormS:{
        attr_name: "",
      }
    };
  },
  mounted() {
    this.getCategories();
  },
  methods: {
    async getCategories() {
      let res = await get_categories();
      console.log(res);
      this.options = res.data;
    },
    handleClick(tab) {
      // console.log(tab.name);
      if (this.kong.length == 3) {
        this.hj_kong(tab.name);
      }
    },
    // 获取参数
    get_click() {
      if (this.kong.length == 3) {
        this.hj_kong(this.activeName);
      }
    },
    async hj_kong(sel) {
      let res = await categories_get(this.kong[this.kong.length - 1], { sel });
      // console.log(res);
      res.data.forEach((v) => {
        v.attr_vals =v.attr_vals? v.attr_vals.split(","):[];
        v.inputVisible = false;
        v.inputValue = "";
      });
      console.log(res);

      if (sel == "many") {
        this.tableData = res.data;
      } else {
        this.tableDataS = res.data;
      }
    },
    //删除 标签
    async categories_cha(row) {
      let res = await categories_delete(this.kong[this.kong.length - 1],row.attr_id,row)
      // console.log(res);
      this.getCategories();
    },

    // 
    async handleInputConfirm(row) {
      // console.log(row.attr_sel);

      if (row.inputValue) {
        row.attr_vals.push(row.inputValue);
        // console.log(row.attr_vals);
        var attr_vals = row.attr_vals.join(",");
        let res = await categories_put(
          this.kong[this.kong.length - 1],
          row.attr_id,
          {
            attr_name: row.attr_name,
            attr_sel: row.attr_sel,
            attr_vals: attr_vals,
          }
        );
        console.log(res);
      }
      row.inputVisible = false;
      row.inputValue = "";
    },

    showInput(row) {
      row.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
      this.getCategories();
    },

    // 添加
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          let res = await categories_post(this.kong[this.kong.length - 1], {
            attr_sel: this.activeName,
            attr_name: this.ruleForm.attr_name,
          });
          // console.log(res);
          this.dialogVisible = false;
          this.getCategories();
         
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.dialogVisible = false;
      this.$refs[formName].resetFields();
    },
    // 查询
    // async categories_chaA(row){
    //   console.log(row);
    //   if(row.attr_sel=='many'){
    //     let res =await categories_delete_get(this.kong[this.kong.length - 1],row.attr_id,row.attr_sel)
    //     console.log(res);
    //   }else{
    //     let res =await categories_delete_get(this.kong[this.kong.length - 1],row.attr_id,row.attr_sel)
    //     console.log(res);
    //   }
      
    // },
    // 
    async handleClose(row,i){
      console.log(row);
      row.attr_vals.splice(i,1)
      var attr_vals = row.attr_vals.join(",");
        let res = await categories_put(
          this.kong[this.kong.length - 1],
          row.attr_id,
          {
            attr_name: row.attr_name,
            attr_sel: row.attr_sel,
            attr_vals: attr_vals,
          }
        );
        this.getCategories();
    },
    categories_bj(row){
      this.dialogVi = true;
       let {attr_id,attr_name,attr_sel,attr_vals}=row
       this.ruleFormS.attr_id=attr_id
       this.ruleFormS.attr_name=attr_name
       this.ruleFormS.attr_sel=attr_sel
       this.ruleFormS.attr_vals=attr_vals
    },

    // 编辑
    submitFormS(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          let attr_vals=this.ruleFormS.attr_vals.join(',')
          let res = await categories_put(this.kong[this.kong.length - 1], this.ruleFormS.attr_id, {
            attr_sel: this.ruleFormS.attr_sel,
            attr_name: this.ruleFormS.attr_name,
            attr_vals:attr_vals
          });
          console.log(res);
          this.dialogVi = false;
          this.getCategories();
         
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  computed: {
    jin() {
      return this.kong.length == 3 ? false : true;
    },
  },
};
</script>
<style lang="scss" >
.el-cascader-menu {
  height: 300px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>